import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new Router({
  routes: [
    {
      path: '/',
      redirect: 'login',
      meta:{
        title:'登录'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: ()=> import('@/pages/register.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/pages/login.vue')
    },
    {
      path:'/complaint',
      name: 'complaint',
      component: ()=>import('@/pages/currency/complaint')
    },
    {
      path:'/ranking',
      name: 'ranking',
      component: ()=>import('@/pages/currency/ranking')
    },
    {
      path: '/student',
      name:'student',
      redirect: '/student/index',
      component: () => import('@/pages/user/index.vue'),
      beforeEnter:(to,from,next)=>{//路由身份判断
        let role=JSON.parse(sessionStorage.getItem('role'))
        if(role==2){
          next()
        }else {
          next({name:'failed'})
        }
      },
      children:[
        {
          path:'index',
          name: 'index',
          component: () => import('@/pages/user/modalvisi')
        },
        {
          path:'people-center',
          name: 'people-center',
          component: () => import('@/pages/user/people-center')
        },
        {
          path: 'detail',
          name:'detail',
          component:()=>import('@/pages/user/detail')
        }
      ]
    },
    {
      path: '/company',
      name: 'company',
      redirect:'/company/index',
      component: ()=>import('@/pages/enterprise/index'),
      beforeEnter:(to,from,next)=>{
        let role=JSON.parse(sessionStorage.getItem('role'))
        if(role==1){
          next()
        }else {
          console.log('failed')
          next({name:'failed'})
        }
      },
      children:[
        {
          path: 'companyMessage',
          name: 'company-message',
          component:()=>import('@/pages/enterprise/company-message')
        },
        {
          path: 'index',
          name:'companyIndex',
          component:()=>import('@/pages/enterprise/company-index')
        },
        {
          path:'file',
          name:'file',
          component:()=>import('@/pages/enterprise/file')
        }
      ]
    },
    {
      path:'*',
      name:'failed',
      component:()=>import('@/pages/currency/lostfile')
    },
    {
      path:'/root',
      name:'rootIndex',
      component:()=>import('@/pages/school/index'),
      beforeEnter:(to,from,next)=>{
        let role=JSON.parse(sessionStorage.getItem('role'))
        if(role==0){
          next()
        }else {
          // console.log('failed')
          next({name:'failed'})
        }
      },
      children:[
        {
          path:'project-massage',
          name:'project-massage',
          component:()=>import('@/pages/school/project-massage')
        },
        {
          path:'use-massage',
          name:'use-massage',
          component:()=>import('@/pages/school/use-massage')
        },
        {
          path:'suggestions',
          name:'suggestions',
          component:()=>import('@/pages/school/suggestions')
        },
        {
          path:'examine',
          name:'examine',
          component:()=>import('@/pages/school/examine')
        }
      ]
    }
  ]
})
// 路由监控跳转页面
router.beforeEach((to, from, next) => {
  let _this = Vue.prototype
  let userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
  let token = JSON.parse(sessionStorage.getItem('token'))
  // document.title=to.meta.title
  if (userInfo && token) {
    next()
  } else if (to.path === '/login') {

    next()
  } else if(to.path === '/register'){
    next()
  } else {
    _this.$message({
      message: '请登录',
      type: 'error',
      center: true,
      onClose () {
        next('/login')
      }
    })
  }
})
// const originalPush = router.prototype.push
// router.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }
export default router
